<?php
session_start();
error_reporting(E_ALL);
include_once 'include_classes.php';

//Connection bdd
$db=new DbConnection();
$db->OpenConnection();

$asso=$_GET['asso'];
$sem=$_POST['semestre'];

//Requete pour le num du trésorier
$sql = "SELECT id FROM membreasso MA, apourrole AP WHERE MA.semestre ='".$sem."' AND MA.association='".$asso."' AND AP.membreasso=MA.id AND AP.role='Trésorier';";
//echo $sql;
$querySql= new DbQuery($db,$sql);
$querySql->SendQuery();
$result=$querySql->GetResult();
$num_treso=$result['id'];

//Calcul des dépenses
$sql = "SELECT SUM(montant) AS depenses FROM membreasso MA, transaction T WHERE MA.association='".$asso."' AND T.tresorier=MA.id AND T.tresorier=".$num_treso." AND T.type<>'Recette';";
//echo $sql;
$querySql= new DbQuery($db,$sql);
$querySql->SendQuery();
$result=$querySql->GetResult();
$depenses=$result['depenses'];

//Calcul des recettes
$sql = "SELECT SUM(montant) AS recettes FROM membreasso MA, transaction T WHERE MA.association='".$asso."' AND T.tresorier=MA.id AND T.tresorier=".$num_treso." AND T.type='Recette';";
//echo $sql;
$querySql= new DbQuery($db,$sql);
$querySql->SendQuery();
$result=$querySql->GetResult();
$recettes=$result['recettes'];

$bilan=$recettes-$depenses;
//echo $depenses."  ".$recettes."  ".$bilan;
//Redirection
echo'<script LANGUAGE="JavaScript">document.location.href="tresorerie.php?asso='.$asso.'&bilan='.$bilan.'&sem='.$sem.'"</script>';

?>
